package cn.com.ylpw.web.order.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import cn.com.ylpw.utils.JsonUtils;
import cn.com.ylpw.web.order.service.saveOrderService;
import cn.com.ylpw.web.util.RestTemplateUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;

@Service("saveOrderService")
public class saveOrderServiceImpl implements saveOrderService {
	public final Logger log = LoggerFactory.getLogger(getClass());
	public static final String CARDNO_FORMAT_URL = "http://order.228/order/verifyId"; // 查询身份证格式
	public static final String RUSH_SEAT_INFO = "http://order.228/public/time/"; // 先付先抢倒计时地址

	/**
	 * 
	 * @description 向第三方接口发送xml数据请求(不带参数)
	 * @param xmlData
	 * @return
	 */
	public Map<String, Object> rushSeatClientPost(String clinentUrl) {
		RestTemplate restTemplate = RestTemplateUtil.getInstance();
		int result;
		String repXml="";
		HashMap<String,Object> repMap = Maps.newHashMap();
		try {
			ResponseEntity<String> responseEntity = restTemplate.postForEntity(clinentUrl, null, String.class);
			result = responseEntity.getStatusCode().value();
			repXml = responseEntity.getBody();
			repMap.put("result", result);
		    repMap.put("jsonData", repXml);
		} catch (Exception e) {
			log.error("向第三方接口发送xml数据请求失败{}",clinentUrl, e);
		}
		return repMap;
	}

	@Override
	/**
	 * 
	 * @description 向第三方接口发送xml数据请求
	 * @param xmlData
	 * @return
	 */
	public Map<String, Object> saveOrderClientPost(Map<String, Object> paramMap, String clinentUrl) {

		RestTemplate restTemplate = RestTemplateUtil.getInstance();
		HttpHeaders requestHeaders = new HttpHeaders();
		LinkedMultiValueMap<String,Object> vars = new LinkedMultiValueMap<String, Object>();
		for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
			 vars.add(entry.getKey(),  entry.getValue().toString());
	    }
		HttpEntity<Object> entity = new HttpEntity<Object>(vars, requestHeaders);
		int result;
		String repXml="";
		HashMap<String,Object> repMap = Maps.newHashMap();
		try {
			ResponseEntity<String> responseEntity = restTemplate.postForEntity(clinentUrl, entity, String.class);
			result = responseEntity.getStatusCode().value();
			repXml = responseEntity.getBody();
			repMap.put("result", result);
		    repMap.put("jsonData", repXml);
		} catch (Exception e) {
			log.error("向第三方接口发送xml数据请求失败{},{}",paramMap, clinentUrl,e);
		}
		return repMap;
	}

	@Override
	public Map<String, Object> getGenOrderInfoMap(Map<String, Object> paramMap, String url) {
		Map<String, Object> retMap = this.saveOrderClientPost(paramMap, url);
		Integer result = (Integer) retMap.get("result");
		if (result != null && result == 200) {
			String retJson = (String) retMap.get("jsonData");
			ObjectMapper mapper = new ObjectMapper();
			try {
				Map<String, Object> map = mapper.readValue(retJson, Map.class);
				Map<String, Object> orderMap = (Map<String, Object>) map.get("result");
				if (orderMap != null && orderMap.size() > 0) {
					return orderMap;
				} else {
					return null;
				}
			} catch (Exception e) {
				 log.error("下单接口请求失败{},{}",paramMap,url, e);
				 return null;
			}
		} else {
			// 请求失败
			try {
				return JsonUtils.parseMap(retMap.get("jsonData").toString());
			} catch (Exception e) {
				log.error("生成订单失败,失败参数:{}",paramMap,e);
				return null;
			}
		}
	}

	@Override
	public Map<String, Object> getCardnoBuyProduct(Map<String, Object> paramMap) {
		Map<String, Object> retMap = this.saveOrderClientPost(paramMap, CARDNO_FORMAT_URL);
		Integer result = (Integer) retMap.get("result");
		if (result != null && result == 200) {
			String retJson = (String) retMap.get("jsonData");
			ObjectMapper mapper = new ObjectMapper();
			try {
				Map<String, Object> map = mapper.readValue(retJson, Map.class);
				Map<String, Object> orderMap = (Map<String, Object>) map.get("result");
				if (orderMap != null && orderMap.size() > 0) {
					return orderMap;
				} else {
					return null;
				}
			} catch (Exception e) {
				 log.warn("查询身份证是否可用异常{}",paramMap, e);
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 查询抢座商品倒计时
	 * 
	 * @author 于旺
	 * @Description: 查询抢座商品倒计时
	 * @param productId
	 *            商品编号
	 * @return: Map<String, Object>
	 */
	@Override
	public Map<String, Object> getRushProductInfo(String productId) {
		Map<String, Object> retMap = this.rushSeatClientPost(RUSH_SEAT_INFO + productId);
		Integer result = (Integer) retMap.get("result");
		if (result != null && result == 200) {
			String retJson = (String) retMap.get("jsonData");
			ObjectMapper mapper = new ObjectMapper();
			try {
				Map<String, Object> map = mapper.readValue(retJson, Map.class);
				Map<String, Object> rushMap = (Map<String, Object>) map.get("result");
				Map<String, Object> rushDate = (Map<String, Object>) rushMap.get("data");
				if (rushDate != null && rushDate.size() > 0) {
					return rushDate;
				} else {
					return null;
				}
			} catch (Exception e) {
				log.error("先付先抢倒计时返回格式错误,返回数据为:{}",retJson);
				return null;
			}
		} else {
			log.error("先付先抢倒计时请求失败,请求返回值:{}",result);
			return null;
		}

	}
}
